// 获取画布和上下文
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');

// 画布尺寸
const CANVAS_WIDTH = 800;
const CANVAS_HEIGHT = 600;
canvas.width = CANVAS_WIDTH;
canvas.height = CANVAS_HEIGHT;

// 玩家数量
const PLAYER_COUNT = 20;
// 玩家数组
let players = [];
// 幸存者数量元素
const survivorNumberElement = document.getElementById('survivor-number');

// 玩家构造函数
function Player(x, y) {
    this.x = x;
    this.y = y;
    this.radius = 10;
    this.speed = 2;
    this.alive = true;
    this.color = getRandomColor();
}

// 获取随机颜色
function getRandomColor() {
    const letters = '0123456789ABCDEF';
    let color = '#';
    for (let i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
}

// 初始化玩家
function initPlayers() {
    for (let i = 0; i < PLAYER_COUNT; i++) {
        const x = Math.random() * (CANVAS_WIDTH - 20) + 10;
        const y = Math.random() * (CANVAS_HEIGHT - 20) + 10;
        players.push(new Player(x, y));
    }
    survivorNumberElement.textContent = PLAYER_COUNT;
}

// 绘制玩家
function drawPlayers() {
    players.forEach(player => {
        if (player.alive) {
            ctx.beginPath();
            ctx.arc(player.x, player.y, player.radius, 0, Math.PI * 2);
            ctx.fillStyle = player.color;
            ctx.fill();
            ctx.closePath();
        }
    });
}

// 移动玩家
function movePlayers() {
    players.forEach(player => {
        if (player.alive) {
            const dx = Math.random() * 2 - 1;
            const dy = Math.random() * 2 - 1;
            player.x += dx * player.speed;
            player.y += dy * player.speed;

            // 边界检测
            if (player.x - player.radius < 0) {
                player.x = player.radius;
            } else if (player.x + player.radius > CANVAS_WIDTH) {
                player.x = CANVAS_WIDTH - player.radius;
            }
            if (player.y - player.radius < 0) {
                player.y = player.radius;
            } else if (player.y + player.radius > CANVAS_HEIGHT) {
                player.y = CANVAS_HEIGHT - player.radius;
            }
        }
    });
}

// 检测玩家碰撞
function checkCollisions() {
    for (let i = 0; i < players.length; i++) {
        const player1 = players[i];
        if (!player1.alive) continue;
        for (let j = i + 1; j < players.length; j++) {
            const player2 = players[j];
            if (!player2.alive) continue;
            const dx = player2.x - player1.x;
            const dy = player2.y - player1.y;
            const distance = Math.sqrt(dx * dx + dy * dy);
            if (distance < player1.radius + player2.radius) {
                // 随机决定谁死亡
                if (Math.random() < 0.5) {
                    player1.alive = false;
                } else {
                    player2.alive = false;
                }
            }
        }
    }
    const alivePlayers = players.filter(player => player.alive);
    survivorNumberElement.textContent = alivePlayers.length;
    if (alivePlayers.length === 1) {
        alert(`游戏结束，幸存者是颜色为 ${alivePlayers[0].color} 的玩家！`);
    }
}

// 主游戏循环
function gameLoop() {
    ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);

    drawPlayers();
    movePlayers();
    checkCollisions();

    requestAnimationFrame(gameLoop);
}

// 初始化游戏
initPlayers();
gameLoop();